package com.gee.spring.cloud.aerospace.simulate.core.satellite.telemetry.task;

import com.gee.spring.cloud.aerospace.simulate.core.satellite.telemetry.common.SateTraceContext;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author gee
 * @since 2025/5/15 10:39
 */
@Slf4j
public class TmArcHandler extends AbstractHandler {

    private TmFrameTask frameTask;

    public TmArcHandler(List<AbstractHandler> delegateTaskList) {
        super(delegateTaskList);
    }

    @Override
    protected boolean doStop(SateTraceContext sateTraceContext, long simulateTime) {
        log.info("TmArcHandler doStop");
        sateTraceContext.getTmArcTimer().stop();
        return true;
    }

    @Override
    protected boolean doStart(SateTraceContext sateTraceContext, long simulateTime) {
        log.info("TmArcHandler doStart");
        long startTime = sateTraceContext.getTraceBeginTime().toEpochMilli();
        long endTime = sateTraceContext.getTraceEndTime().toEpochMilli();
        HashedWheelTimer tmArcTimer = sateTraceContext.getTmArcTimer();
        if (simulateTime <= startTime){
            Timeout arcStartTimeout = tmArcTimer.newTimeout(sateTraceContext.getTmArcStartTask(), startTime - simulateTime, TimeUnit.MILLISECONDS);
            sateTraceContext.setTmArcStartTimeout(arcStartTimeout);
            Timeout arcEndTimeout = tmArcTimer.newTimeout(sateTraceContext.getTmArcEndTask(), endTime - simulateTime, TimeUnit.MILLISECONDS);
            sateTraceContext.setTmArcEndTimeout(arcEndTimeout);
        }else if (simulateTime < endTime){
            Timeout arcEndTimeout = tmArcTimer.newTimeout(sateTraceContext.getTmArcEndTask(), endTime - simulateTime, TimeUnit.MILLISECONDS);
            sateTraceContext.setTmArcEndTimeout(arcEndTimeout);
        }
        return true;
    }

    @Override
    protected boolean doResume(SateTraceContext sateTraceContext, long simulateTime) {
        log.info("TmArcHandler doResume");
        long startTime = sateTraceContext.getTraceBeginTime().toEpochMilli();
        long endTime = sateTraceContext.getTraceEndTime().toEpochMilli();
        HashedWheelTimer tmArcTimer = sateTraceContext.getTmArcTimer();
        if (simulateTime <= startTime){
            log.info("TmArcHandler register start and end");
            Timeout arcStartTimeout = tmArcTimer.newTimeout(sateTraceContext.getTmArcStartTask(), startTime - simulateTime, TimeUnit.MILLISECONDS);
            sateTraceContext.setTmArcStartTimeout(arcStartTimeout);
            Timeout arcEndTimeout = tmArcTimer.newTimeout(sateTraceContext.getTmArcEndTask(), endTime - simulateTime, TimeUnit.MILLISECONDS);
            sateTraceContext.setTmArcEndTimeout(arcEndTimeout);
        }else if (simulateTime < endTime){
            log.info("TmArcHandler register end");
            Timeout arcEndTimeout = tmArcTimer.newTimeout(sateTraceContext.getTmArcEndTask(), endTime - simulateTime, TimeUnit.MILLISECONDS);
            sateTraceContext.setTmArcEndTimeout(arcEndTimeout);
        }
        return true;
    }

    @Override
    protected boolean doPause(SateTraceContext sateTraceContext, long simulateTime) {
        log.info("TmArcHandler doPause");
        Optional.of(sateTraceContext.getTmArcStartTimeout()).ifPresent(Timeout::cancel);
        Optional.of(sateTraceContext.getTmArcEndTimeout()).ifPresent(Timeout::cancel);
        return true;
    }
}
